package com.gooagoo.supercrm.base;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.gooagoo.supercrm.R;
import com.gooagoo.supercrm.utils.ToastUtil;
import com.gyf.barlibrary.ImmersionBar;
import com.umeng.message.PushAgent;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by admin on 2016/11/18.
 */

public abstract class BaseActivity extends Activity {

    public Context mContext;
    public LayoutInflater mInflater;
    protected ImmersionBar immersionBar;
    private CompositeDisposable mCompositeDisposable;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 设置为竖屏
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        setContentView(setContentView());
        mContext = this;
        mInflater = LayoutInflater.from(mContext);
        ButterKnife.bind(this);
        immersionBar = ImmersionBar.with(this).fitsSystemWindows(true).statusBarColor(R.color.main_blue);
        immersionBar.init();
        init();
        PushAgent.getInstance(mContext).onAppStart();
    }

    /**
     * 设置显示布局
     *
     * @return layout资源ID
     */
    protected abstract int setContentView();

    /**
     * 入口
     */
    protected abstract void init();

    public void showToast(String msg) {
        ToastUtil.showToast(mContext, msg);
    }

    public void showToast(int resId) {
        ToastUtil.showToast(mContext, resId);
    }

    public void showErrorToast() {
        ToastUtil.showServerErrorDefault(mContext);
    }




    /**
     * 跳转Activity
     *
     * @param cls
     */
    public void startActivity(Class<?> cls, Bundle bundle) {
        Intent intent = new Intent(mContext, cls);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
    }

    /**
     * 显示指定容器中的某一个View
     *
     * @param container
     * @param resId
     */
    public void showView(List<View> container, int resId) {
        for (View view : container) {
            view.setVisibility(view.getId() == resId ? View.VISIBLE : View.GONE);
        }
    }

    /**
     * 显示指定容器中的某一个View
     *
     * @param container
     * @param v
     */
    public void showView(List<View> container, View v) {
        for (View view : container) {
            view.setVisibility(view.getId() == v.getId() ? View.VISIBLE : View.GONE);
        }
    }

    public void showView(ViewGroup container, View v) {
        int childCount = container.getChildCount();
        for (int i = 0; i < childCount; i++) {
            View childAt = container.getChildAt(i);
            childAt.setVisibility(childAt.getId() == v.getId() ? View.VISIBLE : View.GONE);
        }
    }

    public String getText(TextView textView) {
        return textView.getText().toString();
    }

    /**
     * 判断两个字符串是否相等
     *
     * @param a
     * @param b
     * @return
     */
    public boolean equals(CharSequence a, CharSequence b) {
        return TextUtils.equals(a, b);
    }

    public boolean isEmpty(CharSequence str) {
        return TextUtils.isEmpty(str);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        immersionBar.destroy();
    }


    public <T> void addSubscription(Observable<T> observable, DisposableObserver<T> observer) {
        if (mCompositeDisposable == null) {
            mCompositeDisposable = new CompositeDisposable();
        }
        mCompositeDisposable.add(observer);

        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    public void addSubscription(Disposable disposable) {
        if (mCompositeDisposable == null) {
            mCompositeDisposable = new CompositeDisposable();
        }
        mCompositeDisposable.add(disposable);
    }

    public void onUnsubscribe() {
        Log.d("TAG","onUnSubscribe");
        //取消注册，以避免内存泄露
        if (mCompositeDisposable != null)
            mCompositeDisposable.dispose();
    }
}
